/**
 * ColourSchemeKey.java
 * colourmap
 * Copyright (C) 2010 Alan W.F. Boyd
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * You can contact the author via the google code web page for the project
 * <http://colourmap.googlecode.com>
 */
package com.googlecode.colourmap;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.googlecode.colourmap.colours.IColourScheme;


/**
 * The ColourSchemeKey is a visual display to the user that maps between
 * data values and colours
 * 
 * @author Alan W.F. Boyd
 */
public class ColourSchemeKey extends JFrame
{
	/** The size of the border around the JFrame */
	private static final int BORDER = 25;
	
	/** Serial number for this class version */
	private static final long serialVersionUID = 4693369900052580791L;
	
	/** The minimum data value */
	private float minValue;
	
	/** The maximum data value */
	private float maxValue;
	
	/** The colour scheme that provides the mapping from data values to colours */
	private IColourScheme scheme;
	
	/**
	 * A constructor for the key
	 * @param min the minimum value value
	 * @param max the maximum data value
	 */
	public ColourSchemeKey(float min, float max)
	{
		super("Key");
		minValue = min;
		maxValue = max;
	}
	
	/**
	 * Changes the minimum data value for the key
	 * @param f new minimum data value
	 */
	public void setMin(float f)
	{
		minValue = f;
	}
	
	/**
	 * Changes the maximum data value for the key
	 * @param f new maximum data value
	 */
	public void setMax(float f)
	{
		maxValue = f;
	}
	
	/**
	 * Paints the window showing the key
	 * @param gfx the graphics context
	 */
	public void paint(Graphics gfx)
	{
		Dimension d = getSize();
		
		gfx.clearRect(0, 0, (int)d.getWidth(), (int)d.getHeight());
		
		int maxWidth = Integer.MIN_VALUE;
		
		for (int i = 0; i <= 10; i++)
		{
			float thisPoint = minValue + (maxValue-minValue)*i/10;
			
			gfx.setColor(scheme.getColour(thisPoint));
			gfx.fillRect(10, 30+i*25, 25, 20);
			
			gfx.setColor(Color.black);
			gfx.drawString(thisPoint+"", 45, 43+i*25);
			
			int thisWidth = SwingUtilities.computeStringWidth(gfx.getFontMetrics(), thisPoint+"");
			if (thisWidth > maxWidth)
				maxWidth = thisWidth;
		}
		
		setSize(new Dimension(45+maxWidth+BORDER, 43+275+BORDER));
	}
	
	/**
	 * Sets the colour scheme to use
	 * @param s the new colour scheme
	 */
	public void setColourScheme(IColourScheme s)
	{
		scheme = s;
	}
}
